
$(document).ready(function () {

    //穿梭框数据展示
    $('#transferContainer').transfer({
        titles: ['可选权限', '已有权限'],
        search: true,
        uniqueId: "id",
        dataSource: [],
        selectdataSource: [],
        unselectColumns: [{
            field: 'flag',
            checkbox: true
        }, {
            field: 'name',
            title: '权限名称'
        }],
        selectColumns: [{
            field: 'flag',
            checkbox: true
        }, {
            field: 'name',
            title: '权限名称'
        }]
    });


    //获取用户组数据
    firstdata();
    //获取父模块数据
    seconddata();

});

function getTransfer() {
    //用户组	
    var getCode = $('#zerolevel option:selected').val();
    //父模块
    var getId = $('#onelevel option:selected').val();
    $.ajax({
        //					url: DOMAIN + "/v1/resources",
        url: DOMAIN + "/v1/selectByCode" + isLogin,
        type: "GET",
        dataType: "json",
        data: {
            'code': getCode,
            'parentId': getId
        },
        success: function (result) {
            //获取左侧未选权限列表
            var data = result.data.listUnSel;
            var finaldata = [];
            $.each(data, function (i, val) {
                var newdata = {};
                newdata.id = val.id;
                newdata.name = val.name;
                finaldata.push(newdata);
            });
            //显示右侧已选择权限列表
            $.ajax({
                url: DOMAIN + "/v1/selectByCode" + isLogin,
                type: "GET",
                dataType: "json",
                data: {
                    'code': getCode,
                    'parentId': getId
                },
                success: function (result2) {
                    //显示右侧已选择权限列表
                    var data2 = result2.data.listSel;
                    var finaldata2 = [];
                    $.each(data2, function (i, val) {
                        var newdata2 = {};
                        newdata2.id = val.id;
                        newdata2.name = val.name;
                        finaldata2.push(newdata2);
                    });

                    //穿梭框显示数据左右刷新
                    $('#transferContainer').transfer('refreshLeft', finaldata);
                    $('#transferContainer').transfer('refreshRight', finaldata2);
                },
                error: function (r) {
                    console.log(r);
                }
            });


        },
        error: function (r) {
            console.log(r);
        }
    });


};

//用户组查询
$("#zerolevel").on("change", function () {
    //穿梭框数据置空
    $('#transferContainer').transfer('refreshLeft', '');
    $('#transferContainer').transfer('refreshRight', '');
    //用户组	
    var getCode = $('#zerolevel option:selected').val();
    //父模块
    var getId = '';
    //					console.log(getCode);
    //					console.log(getId);

    //穿梭框数据展示
    getTransfer()

});

//父模块查询
$("#onelevel").on("change", function () {
    //穿梭框数据置空
    $('#transferContainer').transfer('refreshLeft', '');
    $('#transferContainer').transfer('refreshRight', '');
    //用户组	
    var getCode = $('#zerolevel option:selected').val();
    //父模块
    var getId = $('#onelevel option:selected').val();
    //必须选择用户组再选择父模块
    if (getCode == '') {
        alert('请先选择用户组');
        $('#onelevel').val('').trigger('chosen:updated');
    } else {
        //穿梭框数据展示
        getTransfer()
    }

});

$('#btn').click(function () {
    //用户组	
    var getBigId = $('#zerolevel option:selected').attr('id');

    //父模块
    var getParentId = $('#onelevel option:selected').val();

    var data = $('#transferContainer').transfer('getData', 'selectData', 'id');

    var finaldata = [];
    $.each(data, function (i, val) {
        var newdata = {};
        newdata.resourceId = val;
        finaldata.push(newdata);
    });

    $.ajax({
        contentType: "application/json",
        url: DOMAIN + "/v1/insertLinkRoleResource" + isLogin,
        type: "POST",
        dataType: "json",
        data: JSON.stringify({
            "roleId": getBigId,
            "parentId": getParentId,
            "listSel": finaldata
        }),

        success: function (result) {
            alert('权限添加成功')
        },
        error: function (r) {
            console.log(r);
        }
    });
})



//穿梭框数据展示--end

//选择用户组
//获取用户组数据
function firstdata() {
    $.ajax({
        url: DOMAIN + "/v1/roles" + isLogin,
        type: "GET",
        dataType: "json",
        data: {},
        success: function (result) {
            var data = result.data;
            for (var i = 0; i < data.length; i++) {
                var html =
                    '<option value=' + data[i].code + ' id=' + data[i].id + '>' + data[i].name + '</option>';

                $('#zerolevel').append(html);
            }
        },
        error: function (r) {
            console.log(r);
        }
    });
}

//获取父模块数据
function seconddata() {
    //用户组	
    var getCode = $('#zerolevel option:selected').val();
    //父模块
    var getId = $('#onelevel option:selected').val();

    $.ajax({
        url: DOMAIN + "/v1/resources" + isLogin,
        type: "GET",
        dataType: "json",
        data: {
            'parentId': '1'
        },
        success: function (result) {
            var data = result.data;
            for (var i = 0; i < data.length; i++) {
                var html =
                    '<option value=' + data[i].id + '>' + data[i].name + '</option>';

                $('#onelevel').append(html);
            }
        },
        error: function (r) {
            console.log(r);
        }
    });
}
